package com.atguigu.edu.publisher.controller;


import com.atguigu.edu.publisher.beans.TrafficKeywordsBean;
import com.atguigu.edu.publisher.service.KeywordRateService;
import com.atguigu.edu.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class KeywordsRateController {
    @Autowired
    KeywordRateService keywordRateService;

    @RequestMapping("/keywords")
    public String getKeywordsScore(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<TrafficKeywordsBean> keyWords = keywordRateService.getKeyWords(date);
        StringBuilder res = new StringBuilder("{\"status\": 0,\"data\": [");
        for (int i = 0; i < keyWords.size(); i++) {
            TrafficKeywordsBean trafficKeyword = keyWords.get(i);
            res.append("{\"name\": \""+ trafficKeyword.getKeyword() +"\",\"value\": "+ trafficKeyword.getKeywordScore() +"}");
            if (i<keyWords.size() - 1){
                res.append(",");
            }
        }
        res.append("]}");
        return res.toString();
    }
}
